package com.archtecture.model.patterns;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Toast;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.utils.AndroidUtil;

@SuppressWarnings("rawtypes")
public abstract class PadraoActivityCad extends PadraoActivity implements Button.OnClickListener, OnItemSelectedListener {

	protected abstract void configurarEditar(Integer pCodigo) throws NegocioException;

	@Override
	protected void configurarManutencao() throws NegocioException {

		try {

			Integer lCodigo = getIntent().getIntExtra("CODIGO", 0);
			if (lCodigo != null && lCodigo != 0) {
				configurarEditar(lCodigo);
			}

		} catch (NegocioException e) {
			Toast.makeText(this, AndroidUtil.obterMensagemErro(e), Toast.LENGTH_SHORT).show();
		}
	}

	protected void executarClick(View pView) throws NegocioException {}

	protected void executarSelecao(AdapterView<?> pParent, View pView, int pPosition, long pID) throws NegocioException {}

	protected abstract Class<?> getControleManutencao();

	@Override
	public void onClick(final View pView) {

		try {
			executarClick(pView);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), AndroidUtil.obterMensagemErro(e), Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> pParent, View pView, int pPosition, long pID) {
		try {
			executarSelecao(pParent, pView, pPosition, pID);
		} catch (final NegocioException e) {
			Toast.makeText(pView.getContext(), e.getMensagemErro().toString(), Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {}

}
